<template>
    <d2-container>
        <!-- 搜索表头 -->
        <template slot="header">
            <el-form :inline="true" :model="searchForm" ref="searchForm" size="small" v-if="showHeader"
                class="searchHeader">

                <el-form-item label="所属集团" prop="pkGroup">
                    <ec-group-tree-select ref="groupSelectTree" :visible="showHeader" v-model="searchForm.pkGroup">
                    </ec-group-tree-select>
                </el-form-item>

                <el-form-item label="所属组织" prop="pkOrg">
                    <ec-org-tree-select ref="orgSelectTree" :visible="showHeader" v-model="searchForm.pkOrg"
                        :pkGroup="searchForm.pkGroup"></ec-org-tree-select>
                </el-form-item>

                <el-form-item label="所属部门" prop="pk_fatherdept">
                    <ec-dept-tree-select ref="deptSelectTree" v-model="searchForm.pkDept" :pkGroup="searchForm.pkGroup"
                        :pkOrg="searchForm.pkOrg" placeholder="请选择部门">
                    </ec-dept-tree-select>
                </el-form-item>

                <el-form-item label="类型" prop="userType">
                    <ec-dict-select v-model="searchForm.userType" placeholder="请选择用户类型" dictType="user_type">
                    </ec-dict-select>
                </el-form-item>

                <el-form-item label="用户名" prop="username">
                    <el-input v-model="searchForm.username" clearable placeholder="请输入用户名" />
                </el-form-item>

                <el-form-item label="手机号" prop="phone">
                    <el-input v-model="searchForm.phone" clearable placeholder="请输入手机号" />
                </el-form-item>

                <el-form-item label="身份证号" prop="idCard">
                    <el-input v-model="searchForm.idCard" clearable placeholder="请输入身分证号" />
                </el-form-item>

                <el-form-item label="操作">
                    <el-button type="primary" @click="getDataTable">
                        <d2-icon name="search" />
                        查询
                    </el-button>
                    <el-button @click="handleFormReset">
                        <d2-icon name="refresh" />
                        重置
                    </el-button>
                </el-form-item>

            </el-form>
        </template>

        <!-- 表格表头 -->
        <div class="table-header">
            <div>
                <el-button type="primary" icon="el-icon-circle-plus-outline" size="small" v-if="isAuth()"
                    @click="addOrUpdate()">添加</el-button>
                <el-dropdown split-button type="primary" @command="genLoginAccount" size="small" v-if="isAuth()"
                    style="margin-left:15px;">
                    生成登陆账号
                    <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item command="PHONE">手机号</el-dropdown-item>
                        <el-dropdown-item command="IDCARD">身份证号</el-dropdown-item>
                        <el-dropdown-item command="EMAIL">邮箱号</el-dropdown-item>
                    </el-dropdown-menu>
                </el-dropdown>
                <!-- <el-button type="primary" icon="el-icon-search" size="small">搜索</el-button> -->
                <!-- <el-button type="primary" icon="el-icon-search" size="small">搜索</el-button> -->
            </div>
            <div>
                <ec-prompt-button content="刷新" icon="el-icon-refresh" @click="getDataTable()"></ec-prompt-button>
                <ec-prompt-button content="搜索" icon="el-icon-search" @click="changeHeader()"></ec-prompt-button>
                <ec-prompt-button content="导入" icon="el-icon-download"></ec-prompt-button>
                <ec-prompt-button content="导出" icon="el-icon-upload2"></ec-prompt-button>
                <ec-prompt-button content="分享" icon="el-icon-share"></ec-prompt-button>
            </div>
        </div>


        <!-- 表格表体 -->
        <ec-table :data="tabledata" @selection-change="handleSelectionChange" v-loading="loading">
            <el-table-column type="selection" header-align="center" align="center" width="50">
            </el-table-column>
            <el-table-column prop="groupName" header-align="center" align="center" show-overflow-tooltip label="所属集团">
            </el-table-column>
            <el-table-column prop="orgName" header-align="center" align="center" show-overflow-tooltip label="所属组织">
            </el-table-column>
            <el-table-column prop="deptName" header-align="center" align="center" show-overflow-tooltip label="所属部门">
            </el-table-column>
            <el-table-column prop="avatar" header-align="center" align="center" show-overflow-tooltip label="头像"
                width="100">
                <template slot-scope="scope">
                    <img :src="scope.row.avatar" min-width="50" height="50" />
                </template>
            </el-table-column>
            <el-table-column prop="username" header-align="center" align="center" show-overflow-tooltip label="员工姓名">
                <template slot-scope="scope">
                    <el-link type="primary" @click="manageHandle(scope.row.userId)">{{scope.row.username}}
                    </el-link>
                </template>
            </el-table-column>
            <el-table-column prop="phone" header-align="center" align="center" show-overflow-tooltip label="手机号">
            </el-table-column>
            <el-table-column prop="idCard" header-align="center" align="center" show-overflow-tooltip label="身份证号">
            </el-table-column>
            <el-table-column prop="lockFlagDesc" header-align="center" align="center" show-overflow-tooltip
                label="账号状态">
            </el-table-column>
            <el-table-column prop="createdTime" header-align="center" align="center" show-overflow-tooltip label="创建时间">
            </el-table-column>
            <el-table-column fixed="right" header-align="center" align="center" width="120" label="操作">
                <template slot-scope="scope">
                    <el-button type="text" size="small" @click="addOrUpdate(scope.row.userId)">修改
                    </el-button>
                    <el-button type="text" size="small" @click="deleteHandle(scope.row.userId)">删除</el-button>
                </template>
            </el-table-column>
        </ec-table>

        <!-- 表格表尾 -->
        <template slot="footer">
            <ec-pagination :page="page" @size-change="handleSizeChange" @current-change="handleCurrentChange">
            </ec-pagination>
        </template>
        <!-- 弹窗, 新增 / 修改 -->
        <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataTable"></add-or-update>
        <manage v-if="manageVisible" ref="manage" @refreshDataList="getDataTable"></manage>
    </d2-container>
</template>

<script>
    import * as userService from '@/api/upms/user'
    import AddOrUpdate from './add-or-update'
    import Manage from './manage'
    export default {
        name: "upms-user-index",
        data() {
            return {
                searchForm: {
                    userType: 'bus',
                    username: '',
                    pkGroup: '',
                    pkOrg: '',
                    pkDept: '',
                    phone: '',
                    idCard: ''
                },
                page: {
                    page: 1,
                    limit: 10,
                    total: 0
                },
                showHeader: true,
                tabledata: [],
                loading: false,
                addOrUpdateVisible: false,
                multipleSelection: [],
                manageVisible: false
            }
        },
        components: {
            AddOrUpdate,
            Manage
        },
        mounted() {
            this.getDataTable()
        },
        methods: {
            // 获取数据列表
            getDataTable() {
                this.loading = true
                let query = {
                    page: this.page.page,
                    limit: this.page.limit,
                    userType: this.searchForm.userType,
                    username: this.searchForm.username,
                    pkGroup: this.searchForm.pkGroup,
                    pkOrg: this.searchForm.pkOrg,
                    pkDept: this.searchForm.pkDept,
                    phone: this.searchForm.phone,
                    idCard: this.searchForm.idCard,
                };
                userService.getuserPage(query).then(data => {
                    this.tabledata = data.list;
                    this.page.total = data.totalCount;
                    this.loading = false;
                }).catch((res) => {
                    this.loading = false;
                });
            },
            // 切换管理页面
            manageHandle(id) {
                this.manageVisible = true
                this.$nextTick(() => {
                    this.$refs.manage.init(id)
                })
            },
            //切换页数
            handleSizeChange(val) {
                this.page.limit = val;
                this.getDataTable();
            },
            // 翻页
            handleCurrentChange(val) {
                this.page.page = val;
                this.getDataTable();
            },
            // 切换显示搜索表头
            changeHeader() {
                this.showHeader = !this.showHeader
            },
            // 重置搜索表单
            handleFormReset() {
                this.$refs.searchForm.resetFields()
                this.$refs.groupSelectTree.resetFields()
                this.$refs.orgSelectTree.resetFields()
                this.$refs.deptSelectTree.resetFields()
            },
            // 新增 / 修改
            addOrUpdate(id) {
                this.addOrUpdateVisible = true
                this.$nextTick(() => {
                    this.$refs.addOrUpdate.init(id)
                })
            },
            // 删除
            deleteHandle(id) {
                if (id) {
                    this.$confirm("确认删除？", "确认信息", {
                        distinguishCancelAndClose: true,
                        confirmButtonText: "删除",
                        cancelButtonText: "取消"
                    }).then(() => {
                        userService.deluser(id).then(() => {
                            this.getDataTable();
                        });
                    });
                }
            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            genLoginAccount(command) {
                const userIds = this.multipleSelection.map(item => {
                    return item.userId
                });

                if (userIds != null && userIds.length > 0) {
                    const params = {
                        type: command,
                        ids: userIds.join()
                    }
                    userService.genLoginAccount(params).then(data => {
                        this.$message({
                            message: '操作成功',
                            type: 'success',
                            duration: 1500
                        })
                    });
                } else {
                    this.$message.warning("请至少选择一项！")
                }


            }
        }
    }
</script>

<style scoped>

</style>